package com.example.kotlinlearn.kotlinLearn

import java.util.*
import kotlin.math.max


fun main(){
    val p = Person(false)
    //次构造函数1
    p.eat()
    val a = Teacher()
    //次构造函数2
    val b = Teacher("csd",23)
    //主构造函数
    val c = Teacher("a",4,false)
    val s = Student("易志豪",22,true)
    s.readBooks()
    s.doHomeWork()
    //多态
    doStudy(s)
    val cell1 = CellPhone("华为",3900)
    val cell2 = CellPhone("小米",3900)
    println((cell1 == cell2).toString() + "----"+cell1.toString()+cell2.toString())
    //调用单例的方法直接使用类名进行调用即可
    SingeLeton.printSingeLet()

    //不可变集合
    val list = listOf("a","b","c","d","e","f","g")
    //可变集合
    val list1  = mutableListOf("a","b","c","d","e","f","g","a")
    //set集合,会去除集合中重复的项，不能放重复的项
    val set = setOf("a","b","c","d","e","f","g","a")
    list1.add("h")
    for (s in set){
        println(s)
    }
    //map集合，存储键值对数据
    val map = hashMapOf<String,Int>()
    map["a"] = 1
    map["b"] = 2
    val num = map["b"]  //读取数据
    //简化写法
    val map1 = mapOf("a" to 1,"b" to 2,"c" to 3)
    println(map1.toString())
    var books = mutableListOf("yi","dahd","dahda","dahdiald","daldha")
    var price = mutableListOf<Int>(15,34,4554,465,57,768,868,87)
    val maxLengthBook = books.maxByOrNull { it.length }
    val maxPrice = price.maxByOrNull { i -> i }
    println("书名最长的那个是：$maxLengthBook"+"-----"+"价格最高的为$maxPrice")
    //全部转为大写
    val newlist = books.map { it.uppercase(Locale.getDefault()) }
    for (i in newlist){
        println(i)
    }
    println("-------------------")
    //对集合中的的元素进行过滤并转换为大写
    val newlist1 = books.filter { it.length <5 }.map { it.uppercase(Locale.getDefault()) }
    for (i in newlist1){
        println(i)
    }
    //any只要集合中有一个元素符合条件就会返回true，all得所有元素满足条件
    val resultValue = books.any { it.length<3 }
    val resultValue1 = books.all { it.length<6 }
    println("any结果是${resultValue}---all的结果是${resultValue1}")

    Thread { println("线程被开启")}.start()
    books.toString()
    var value:Int? = null
    value?.toString()
}

//多态
fun doStudy(study: Study){
    study.doHomeWork()
    study.readBooks()
}
/**
 * 函数基本格式
 */
fun add(num1:Int, num2:Int): Double {
    println(num1+ num2 * 3.343)
    println("打印最大值为:"+max(num1,num2))
    return num1+ num2 * 3.343
}

/**
 * 语法糖，简化代码量，函数体中如只有一行代码，可这样写
 */
fun add1(num1: Int,num2: Int) = max(num1,num2)
fun add2(num1: Int,num2: Int):Int = max(num1,num2)

//if的使用
fun getLarge(num1: Int,num2: Int): Int {
    var value = 0;
    value = if (num1>num2){
        num1
    }else if (num2>num1){
        num2
    }else{
        1
    }
    return value
}
//究极精简的写法
fun getLarge1(num1: Int,num2: Int) = if(num1>num2) num1 else if(num2>num1) num2 else 1

//when语句 返回对应学生的成绩
fun getScore(name:String) = when (name) {"a"->89 "b"->70 "c"-> 78 else ->0}
//when语句中类型匹配,Number抽象类，float,int,double和数字相关的都是它的子类,is关键字
fun cheakNum(num:Number){
    when(num){
        is Int -> println("这是一个int类型的$num")
        is Double -> println("这是一个double类型的数据$num")
        is Float -> println("这是一个float类型的数据$num")
        else -> println("错误！！！")
    }
}
//不传参的when用法
fun getScore1(name: String) = when{
        name =="a" -> 131
        name.startsWith("c") -> 789
        name =="b" -> 67
        else -> 0
    }
//while的使用和java没区别
fun whileFun(){
    var  a = 0
    while (a <1000){
        if (a == 600){
            break
        }
        println(a)
        a++
    }
}

//制造一个从0到10的闭区间
var arr = 0..10
//开区间
var arr1 = 0 until 10
//降序区间
var arr2 = 10 downTo 0
fun forFun(){
    //step关键字每次执行的步数
    for (i in arr2 step 1){
        println(i)
    }
}
